/*
 * Copyright (C) 2009 Georgios J. Georgopoulos, All rights reserved.
 * 
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 * site: http://www.fsf.org.
 */
package org.gwt.mosaic.xul.client.ui;

import java.util.ArrayList;
import java.util.List;

import org.gwt.mosaic.core.client.util.DefaultUnitConverter;
import org.gwt.mosaic.ui.client.layout.BorderLayout;
import org.gwt.mosaic.ui.client.layout.BorderLayoutData;
import org.gwt.mosaic.ui.client.layout.LayoutPanel;
import org.gwt.mosaic.ui.client.layout.BorderLayout.Region;

import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author georgopoulos.georgios(at)gmail.com
 * 
 */
public class Splitter extends Container {
  private static final long serialVersionUID = -1629012269029285413L;

  private transient LayoutPanel layoutPanel = null;

  public static final String COLLAPSE = "collapse";

  public static final String ORIENT = "orient";

  public Splitter() {
    super();
  }

  @Override
  protected Widget createUI() {
    if (layoutPanel == null){
    layoutPanel = new LayoutPanel(new BorderLayout());
    layoutPanel.setPadding(0);
    }

    syncUI(layoutPanel);

    return layoutPanel;
  }
  
  @Override
  protected void syncUI(Widget ui) {
    final List<Element> elementsWithUI = searchForElementsWithUI();

    if ("vertical".equals(getOrient())) {

      layoutPanel.setWidgetSpacing(DefaultUnitConverter.getInstance().dialogUnitYAsPixel(
          VDLU));

      if ("end".equals(getCollapse())) {
        if (elementsWithUI.size() >= 2) {
          layoutPanel.add(elementsWithUI.get(0).createUI());
          layoutPanel.add(elementsWithUI.get(1).createUI(),
              new BorderLayoutData(Region.SOUTH,
                  layoutPanel.getWidgetSpacing(), Integer.MAX_VALUE));
        }
      } else {
        if (elementsWithUI.size() >= 2) {
          layoutPanel.add(elementsWithUI.get(0).createUI(),
              new BorderLayoutData(Region.NORTH,
                  layoutPanel.getWidgetSpacing(), Integer.MAX_VALUE));
          layoutPanel.add(elementsWithUI.get(1).createUI());
        }
      }
    } else {

      layoutPanel.setWidgetSpacing(DefaultUnitConverter.getInstance().dialogUnitXAsPixel(
          HDLU));

      if ("end".equals(getCollapse())) {
        for (int i = 0, n = Math.min(2, elementsWithUI.size()); i < n; ++i) {
          if (elementsWithUI.size() >= 2) {
            layoutPanel.add(elementsWithUI.get(0).createUI());
            layoutPanel.add(elementsWithUI.get(1).createUI(),
                new BorderLayoutData(Region.EAST,
                    layoutPanel.getWidgetSpacing(), Integer.MAX_VALUE));
          }
        }
      } else {
        for (int i = 0, n = Math.min(2, elementsWithUI.size()); i < n; ++i) {
          if (elementsWithUI.size() >= 2) {
            layoutPanel.add(elementsWithUI.get(0).createUI(),
                new BorderLayoutData(Region.WEST,
                    layoutPanel.getWidgetSpacing(), Integer.MAX_VALUE));
            layoutPanel.add(elementsWithUI.get(1).createUI());
          }
        }
      }
    }    
  }

  private List<Element> searchForElementsWithUI() {
    List<Element> elementsWithUI = new ArrayList<Element>();
    for (int i = 0, n = getElementCount(); i < n; ++i) {
      final Element element = getElement(i);
      if (element.getUI() != null) {
        elementsWithUI.add(element);
      }
    }
    return elementsWithUI;
  }

  public String getCollapse() {
    return getString(COLLAPSE);
  }

  public void setCollapse(final String collapse) {
    putString(COLLAPSE, collapse);
  }

  public String getOrient() {
    return getString(ORIENT);
  }

  public void setOrient(final String orient) {
    putString(ORIENT, orient);
  }

}
